//
//  AFNHTTPSessionManager.m
//  Logistics
//
//  Created by zhlzhl on 16/1/15.
//  Copyright © 2016年 zhlzhl. All rights reserved.
//
#import "AFNHTTPSessionManager.h"
#import "HLMBProgressHUD.h"
#import "ConfigureHeader.h"
@implementation AFNHTTPSessionManager
+(instancetype)sharedManager
{
    static AFNHTTPSessionManager * _shareAFNHTTPSessionManager=nil;
    static dispatch_once_t onceTocken;
    dispatch_once(&onceTocken, ^{
        _shareAFNHTTPSessionManager=[[self alloc]initWithBaseURL:[NSURL URLWithString:MAIN_HOST]];
        [_shareAFNHTTPSessionManager.requestSerializer willChangeValueForKey:@"timeoutInterval"];
        _shareAFNHTTPSessionManager.requestSerializer.timeoutInterval=20.f;
        [_shareAFNHTTPSessionManager.requestSerializer didChangeValueForKey:@"timeoutInterval"];
        _shareAFNHTTPSessionManager.responseSerializer.acceptableContentTypes=[NSSet setWithObjects:@"application/json", @"text/json", @"text/javascript",@"text/html",@"text/css",@"text/plain", nil];
    });
    return _shareAFNHTTPSessionManager;
}
+(void)checkNetWorkStatus
{
    /**
     *  AFNetworkReachabilityStatusUnknown          = -1,  // 未知
     *  AFNetworkReachabilityStatusNotReachable     = 0,   // 无连接
     *  AFNetworkReachabilityStatusReachableViaWWAN = 1,   // 3G
     *  AFNetworkReachabilityStatusReachableViaWiFi = 2,   // 局域网络Wifi
     */
    // 如果要检测网络状态的变化, 必须要用检测管理器的单例startMoitoring

    [[AFNetworkReachabilityManager sharedManager] startMonitoring];
    [[AFNetworkReachabilityManager sharedManager] setReachabilityStatusChangeBlock:^(AFNetworkReachabilityStatus status) {
        if (status==0) {
            [MBProgressHUD hideHUDForView:[UIApplication sharedApplication].keyWindow animated:YES];
            [HLMBProgressHUD addMBProgressHUDinView: [UIApplication sharedApplication].keyWindow hudMode:5 hideDelay:1 hudDetailText:@"断网了"];
            
            return ;
        }else if(2==status){
      
//      [HLMBProgressHUD addMBProgressHUDinView: [UIApplication sharedApplication].keyWindow hudMode:5 hideDelay:1 hudDetailText:@"网络已连接!"];
        }
    }];
    
    
}

+(void)postWithParameters:(NSDictionary *)Parameters subUrl:(NSString *)suburl WithReturnValeuBlock:(ReturnValueBlock)returnBlock WithErrorCodeBlock:(ErrorCodeBlock)errorBlock WithFailureBlock:(FailureBlock)failureBlock
{
    NSLog(@"%@",Parameters);
    [self checkNetWorkStatus];
    NSString *urlstring=[MAIN_HOST stringByAppendingString:suburl];
    [[[self class] sharedManager]POST:urlstring parameters:Parameters constructingBodyWithBlock:^(id<AFMultipartFormData>  _Nonnull formData) {
    } progress:^(NSProgress * _Nonnull uploadProgress) {
        
    } success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {
        returnBlock(responseObject);

    } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
        failureBlock();

    }];
    
}
+(void)getInfoWithSubUrl:(NSString *)subUrl parameters:(NSDictionary *)Parameters WithReturnValeuBlock:(ReturnValueBlock)returnBlock WithErrorCodeBlock:(ErrorCodeBlock)errorBlock WithFailureBlock:(FailureBlock)failureBlock{
    [self checkNetWorkStatus];
    NSString *urlstring=[MAIN_HOST stringByAppendingString:subUrl];
    NSLog(@"%@-%@",urlstring,Parameters);
    
    [[[self class] sharedManager]GET:urlstring parameters:Parameters progress:^(NSProgress * _Nonnull downloadProgress) {
    } success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {
        returnBlock(responseObject);
    } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
//   [HLMBProgressHUD addMBProgressHUDinView:[UIApplication sharedApplication].keyWindow hudMode:5 hideDelay:2 hudDetailText:@"网络请求超时"];
        NSLog(@"--%@--",error.userInfo);
        failureBlock();
     

    }];
}
@end
